package com.rois.librois.libRoISComponentClient;

import java.util.logging.Logger;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;

import com.rois.librois.RoIS.ComponentBase;
import com.rois.librois.RoIS.ComponentBaseHelper;

/**
 * Implementation of common interface for CORBA client operation.
 *
 * @author Miki Sato <miki.sato.1225@gmail.com>
 *
 *    The MIT licence
 *
 *    Copyright (C) 2011
 *      ATR Intelligent Robotics and Communication Laboratories, Japan
 *
 *    Permission is hereby granted, free of charge, to any person obtaining a copy
 *    of this software and associated documentation files (the "Software"), to deal
 *    in the Software without restriction, including without limitation the rights
 *    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 *    of the Software, and to permit persons to whom the Software is furnished to do so,
 *    subject to the following conditions:
 *
 *    The above copyright notice and this permission notice shall be included in all
 *    copies or substantial portions of the Software.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 *    INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 *    PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 *    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

public class ClientOperationImpl implements com.rois.librois.libRoISComponentClient.ClientOperation
{
	private ORB m_orb;
	public ComponentBase m_comp_ref;	// ***Component Interface***

	@Override
	public boolean start(
		String nameserver,
		String servername
	)
	{
		boolean bRes = false;
		try{
			// Initialize ORB
			String argv[] = new String[2];
			argv[0] = "-ORBInitRef";
			String strNameService1 = "NameService=corbaloc:iiop:";
			String strNameService2 = "/NameService";
			String strNameService = strNameService1 + nameserver + strNameService2;
			argv[1] = strNameService;
			m_orb = org.omg.CORBA.ORB.init(argv, null);

			// Obtain the Name Service
			org.omg.CORBA.Object obj_ref;
			obj_ref = m_orb.resolve_initial_references("NameService");

			// Narrow the reference of the Name Service
			NamingContextExt nc_ref;
			nc_ref = NamingContextExtHelper.narrow(obj_ref);

			// Search and obtain the reference of the Server
/*
//@@: same as C++ version
			String strServerName1 = "corbaname::";
			String strServerName2 = "/NameService#";
			String strServerName = strServerName1 + nameserver + strServerName2 + servername;
			org.omg.CORBA.Object comp_obj = m_orb.string_to_object(strServerName);
			m_comp_ref = ComponentBaseHelper.narrow(comp_obj);
*/
			m_comp_ref = ComponentBaseHelper.narrow(nc_ref.resolve_str(servername));
		}
		catch(Exception e)
		{
			Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info("failed to connect to Server.");
			return bRes;
		}

		Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info("client has started.");
		bRes = true;
		return bRes;
	}

	@Override
	public boolean stop(
		String servername
	)
	{
		Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info("client has stopped.");
		return false;
	}

}
